home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER6 / ARRANGE.C next >
C/C++ Source or Header  |  1996-03-06  |  16KB  |  458 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "arrange.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "ListView_Arrange()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
  108.  
  109. HIMAGELIST CreateMultiDrag( HWND hList, int nItem, POINT* pHotPoint );
  110.  
  111. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  112. {
  113. static POINT      ptHotSpot, pt;
  114. static int        nDragItem = -1;
  115. static HWND       hList     = NULL;
  116. static HIMAGELIST hCursors  = NULL;
  117. static HIMAGELIST hDragList = NULL;
  118.  
  119.    switch( uMsg )
  120.    {
  121.       case WM_CREATE  :
  122.               {
  123.                  HIMAGELIST hImage;
  124.                  ICONINFO   info;
  125.  
  126.                  InitCommonControls();
  127.  
  128.                  hCursors = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 1, 1 ); 
  129.                  hImage   = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  130.                  hList = CreateWindowEx( 0, WC_LISTVIEW, "",
  131.                                          WS_CHILD | WS_VISIBLE | LVS_ICON,
  132.                                          10, 100, 100, 100,
  133.                                          hWnd,
  134.                                          (HMENU)1,
  135.                                          hInst,
  136.                                          NULL);
  137.  
  138.                  if ( hList )
  139.                  {
  140.                     LV_ITEM item;
  141.                     HBITMAP hBmp;
  142.                     int     i;
  143.  
  144.                     ListView_SetImageList( hList, hImage, LVSIL_NORMAL );
  145.  
  146.                     item.mask = LVIF_TEXT | LVIF_IMAGE;
  147.  
  148.                     for( i=0; i<4; i++ )
  149.                     {
  150.                        hBmp = LoadBitmap( hInst, lpszData[i] );
  151.  
  152.                        item.pszText  = (LPTSTR)lpszData[i];
  153.                        item.iItem    = i;
  154.                        item.iSubItem = 0;
  155.                        item.iImage   = ImageList_AddMasked( hImage, hBmp, RGB( 0, 255, 0 ) );
  156.  
  157.                        ListView_InsertItem( hList, &item );
  158.  
  159.                        DeleteObject( hBmp );
  160.                     }
  161.                  }
  162.  
  163.                  ImageList_AddIcon( hCursors, LoadCursor( NULL, IDC_ARROW ) );
  164.  
  165.                  // Retrieve the hot spot information from the cursor.
  166.                  //...................................................
  167.                  GetIconInfo( LoadCursor( NULL, IDC_ARROW ), &info );
  168.                  ptHotSpot.x = info.xHotspot;
  169.                  ptHotSpot.y = info.yHotspot;
  170.  
  171.                  // Clean up the bitmaps from GetIconInfo().
  172.                  //.........................................
  173.                  DeleteObject( info.hbmMask );
  174.                  DeleteObject( info.hbmColor );
  175.               }
  176.               break;
  177.  
  178.       case WM_SIZE :
  179.               if ( hList )
  180.                  MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  181.  
  182.               if ( wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED )
  183.                  ListView_RedrawItems( hList, 0, ListView_GetItemCount( hList ) );
  184.               break;
  185.  
  186.       case WM_NOTIFY :
  187.               if ( wParam == 1 )
  188.               {
  189.                  NM_LISTVIEW* pNM = (NM_LISTVIEW*)lParam;
  190.  
  191.                  switch ( pNM->hdr.code )
  192.                  {
  193.                     case LVN_BEGINDRAG :                 
  194.                            // Create the drag image.
  195.                            //.......................
  196.                            if ( ListView_GetSelectedCount( hList ) == 1 )
  197.                               hDragList = ListView_CreateDragImage( hList, pNM->iItem, &pt );
  198.                            else
  199.                               hDragList = CreateMultiDrag( hList, pNM->iItem, &pt );
  200.  
  201.                            // Start dragging and set the drag cursor.
  202.                            //........................................
  203.                            ImageList_BeginDrag( hDragList, 0, pNM->ptAction.x-pt.x, 
  204.                                                               pNM->ptAction.y-pt.y );
  205.                            ImageList_SetDragCursorImage( hCursors, 0, pNM->ptAction.x-pt.x-ptHotSpot.x, 
  206.                                                                       pNM->ptAction.y-pt.y-ptHotSpot.y );
  207.                            ImageList_DragEnter( hList, pNM->ptAction.x, pNM->ptAction.y ); 
  208.  
  209.                            // Capture the mouse and hide the normal cursor.
  210.                            //..............................................
  211.                            SetCapture( hWnd );
  212.                            ShowCursor( FALSE );
  213.  
  214.                            pt        = pNM->ptAction;
  215.                            nDragItem = pNM->iItem;
  216.                            break;
  217.                  } 
  218.               }
  219.               break;
  220.  
  221.  
  222.       case WM_MOUSEMOVE :
  223.               // Move the drag cursor if dragging.
  224.               //..................................
  225.               if ( hDragList )
  226.               {
  227.                  ImageList_DragMove( LOWORD( lParam ), HIWORD( lParam ) );
  228.               }
  229.               break;
  230.  
  231.  
  232.       case WM_LBUTTONUP :
  233.               // If dragging, drop the object in its new place.
  234.               //...............................................
  235.               if ( hDragList )
  236.               {
  237.                  POINT ptPos;
  238.  
  239.                  ImageList_DragLeave( hList );
  240.                  ImageList_EndDrag();
  241.                  ImageList_Destroy( hDragList );
  242.                  hDragList = NULL;
  243.                  ReleaseCapture();
  244.                  ShowCursor( TRUE );
  245.  
  246.                  if ( ListView_GetSelectedCount( hList ) == 1 )
  247.                  {
  248.                     ListView_GetItemPosition( hList, nDragItem, &ptPos );
  249.                     ListView_SetItemPosition( hList, nDragItem, ptPos.x+(LOWORD( lParam )-pt.x), ptPos.y+(HIWORD( lParam )-pt.y) );
  250.                  }
  251.                  else // Dragging multiple items.
  252.                  {
  253.                     int nCur = -1;
  254.  
  255.                     while ( (nCur = ListView_GetNextItem( hList, nCur, LVNI_SELECTED )) > -1 )
  256.                     {
  257.                        ListView_GetItemPosition( hList, nCur, &ptPos );
  258.                        ListView_SetItemPosition( hList, nCur, ptPos.x+(LOWORD( lParam )-pt.x), ptPos.y+(HIWORD( lParam )-pt.y) );
  259.                     }
  260.                  }
  261.               }
  262.               break;
  263.  
  264.       case WM_COMMAND :
  265.               switch( LOWORD( wParam ) )
  266.               {
  267.                  case IDM_ARRANGE :
  268.                         ListView_Arrange( hList, LVA_DEFAULT );
  269.                         break;
  270.  
  271.                  case IDM_ABOUT :
  272.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  273.                         break;
  274.  
  275.                  case IDM_EXIT :
  276.                         DestroyWindow( hWnd );
  277.                         break;
  278.               }
  279.               break;
  280.       
  281.       case WM_DESTROY :
  282.               if ( ListView_GetImageList( hList, LVSIL_NORMAL ) )
  283.                  ImageList_Destroy( ListView_GetImageList( hList, LVSIL_NORMAL ) );
  284.  
  285.               PostQuitMessage(0);
  286.               break;
  287.  
  288.       default :
  289.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  290.    }
  291.  
  292.    return( 0L );
  293. }
  294.  
  295.  
  296. LRESULT CALLBACK About( HWND hDlg,           
  297.                         UINT message,        
  298.                         WPARAM wParam,       
  299.                         LPARAM lParam)
  300. {
  301.    switch (message) 
  302.    {
  303.        case WM_INITDIALOG: 
  304.                return (TRUE);
  305.  
  306.        case WM_COMMAND:                              
  307.                if (   LOWORD(wParam) == IDOK         
  308.                    || LOWORD(wParam) == IDCANCEL)    
  309.                {
  310.                        EndDialog(hDlg, TRUE);        
  311.                        return (TRUE);
  312.                }
  313.                break;
  314.    }
  315.  
  316.    return (FALSE); 
  317. }
  318.  
  319.  
  320. HIMAGELIST CreateMultiDrag( HWND hList, int nItem, POINT* pHotPoint )
  321. {
  322.    HBITMAP    hBmp1, hBmp2, hTmp1, hTmp2, hImage1, hImage2;
  323.    HIMAGELIST hImageList;
  324.    HPEN       hPen;
  325.    RECT       rect, rect1, rect2, rect3, rect4;
  326.    HDC        hMemDC1, hMemDC2;
  327.  
  328.    HDC        hDC    = GetDC( hList );
  329.    int        i      = 0;
  330.    int        nCur   = -1;
  331.    int        nSel   = ListView_GetSelectedCount( hList );
  332.  
  333.  
  334.    // Find out the size of the rect that
  335.    // contains all items selected.
  336.    //...................................
  337.    for ( i=0; i<nSel; i++ )
  338.    {
  339.       nCur = ListView_GetNextItem( hList, nCur, LVNI_SELECTED );
  340.  
  341.       ListView_GetItemRect( hList, nCur, &rect1, LVIR_LABEL );
  342.       ListView_GetItemRect( hList, nCur, &rect2, LVIR_ICON  );
  343.       UnionRect( &rect4, &rect1, &rect2 );
  344.  
  345.       if( i == 0 )
  346.          rect3 = rect4;
  347.  
  348.       UnionRect( &rect, &rect3, &rect4 );
  349.       rect3 = rect;
  350.    }
  351.  
  352.    pHotPoint->x = rect.left;
  353.    pHotPoint->y = rect.top;
  354.    
  355.    // Create 2 compatible bitmaps and device contexts,
  356.    // one for the drag image and one for the drag mask.
  357.    //..................................................
  358.    
  359.    hBmp1   = CreateCompatibleBitmap( hDC, rect.right - rect.left, rect.bottom - rect.top );
  360.    hBmp2   = CreateCompatibleBitmap( hDC, rect.right - rect.left, rect.bottom - rect.top );
  361.    hPen    = CreatePen( PS_SOLID, 1, RGB( 128, 128, 128 ) );
  362.    hMemDC1 = CreateCompatibleDC( hDC );
  363.    hMemDC2 = CreateCompatibleDC( hDC );
  364.  
  365.    // Select the empty bitmaps, brushes, and pens 
  366.    // into the compatible device contexts.
  367.    //............................................
  368.    
  369.    SelectObject( hMemDC1, hBmp1 );
  370.    SelectObject( hMemDC2, hBmp2 );
  371.    SelectObject( hMemDC1, GetStockObject( BLACK_BRUSH ) );
  372.    SelectObject( hMemDC2, GetStockObject( WHITE_BRUSH ) );
  373.    SelectObject( hMemDC1, hPen );
  374.    SelectObject( hMemDC2, GetStockObject( BLACK_PEN ) );
  375.  
  376.    rect3.left   -= rect.left;
  377.    rect3.right  -= rect.left;
  378.    rect3.top    -= rect.top;
  379.    rect3.bottom -= rect.top;
  380.  
  381.    // Fill the entire bitmap with the
  382.    // appropriate color.
  383.    //................................
  384.    
  385.    FillRect( hMemDC1, &rect3, (HBRUSH)GetStockObject( BLACK_BRUSH ) );
  386.    FillRect( hMemDC2, &rect3, (HBRUSH)GetStockObject( WHITE_BRUSH ) );
  387.  
  388.    // Draw the images on the bitmaps.
  389.    //................................
  390.  
  391.    nCur = -1;   
  392.    for( i = 0; i < nSel; i++ )
  393.    {
  394.       nCur = ListView_GetNextItem( hList, nCur, LVNI_SELECTED );
  395.  
  396.       ListView_GetItemRect( hList, nCur, &rect2, LVIR_LABEL );
  397.       ListView_GetItemRect( hList, nCur, &rect3, LVIR_ICON  );
  398.  
  399.  
  400.       // Paint the Image.
  401.       //.................
  402.    
  403.       Rectangle( hMemDC1, rect3.left - rect.left, rect3.top - rect.top,
  404.                           rect3.right - rect.left, rect3.bottom - rect.top );
  405.  
  406.       MoveToEx( hMemDC1, rect2.left - rect.left,
  407.                          (rect2.top - rect.top)+
  408.                          ((rect2.bottom - rect.top) - (rect2.top - rect.top))/2, NULL );
  409.  
  410.       LineTo( hMemDC1, rect2.right - rect.left,
  411.                          (rect2.top - rect.top)+
  412.                          ((rect2.bottom - rect.top) - (rect2.top - rect.top))/2 );
  413.  
  414.       // Paint the Mask.
  415.       //................
  416.    
  417.       Rectangle( hMemDC2, rect3.left - rect.left, rect3.top - rect.top,
  418.                           rect3.right - rect.left, rect3.bottom - rect.top );
  419.  
  420.       MoveToEx( hMemDC2, rect2.left - rect.left,
  421.                          (rect2.top - rect.top)+
  422.                          ((rect2.bottom - rect.top) - (rect2.top - rect.top))/2, NULL );
  423.  
  424.       LineTo( hMemDC2, rect2.right - rect.left,
  425.                          (rect2.top - rect.top)+
  426.                          ((rect2.bottom - rect.top) - (rect2.top - rect.top))/2 );
  427.    }
  428.  
  429.    // Create temporary bitmaps and select them
  430.    // into the device context which will then return
  431.    // a handle to the bitmaps that we just made.
  432.    //...............................................
  433.    
  434.    hTmp1   = CreateCompatibleBitmap( hDC, 1, 1 );
  435.    hTmp2   = CreateCompatibleBitmap( hDC, 1, 1 );
  436.    hImage1 = (HBITMAP)SelectObject( hMemDC1, hTmp1 );
  437.    hImage2 = (HBITMAP)SelectObject( hMemDC2, hTmp2 );
  438.  
  439.    // Create the Drag image list and add the images.
  440.    //...............................................
  441.    
  442.    hImageList = ImageList_Create( rect.right-rect.left, rect.bottom-rect.top, TRUE, 1, 1 );
  443.  
  444.    ImageList_Add( hImageList, hImage1, hImage2  );
  445.  
  446.    ReleaseDC( hList, hDC );
  447.    DeleteDC( hMemDC1 );
  448.    DeleteDC( hMemDC2 );
  449.  
  450.    DeleteObject( hBmp1 );
  451.    DeleteObject( hBmp2 );
  452.    DeleteObject( hTmp1 );
  453.    DeleteObject( hTmp2 );
  454.    DeleteObject( hPen );
  455.  
  456.    return( hImageList );
  457. }
  458.